package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Rule;
import cn.edu.scau.cmi.ema.domain.Site;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("RuleController")
public class RuleController extends CmiEmaController {

	@RequestMapping("/listRules")
	public ModelAndView listRules(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = rulePaginationService.updatePagination(request, pagination);
		Set<Rule> rules = rulePaginationService.getPaginationRules(pagination);

		mav.addObject("rules", rules);
		mav.addObject("entityName", "rule");
		mav.addObject("pagination", pagination);
		mav.setViewName("rule/listRules");
		return mav;
	}

	public String listRules() {
		return "redirect:/listRules";
	}

	@RequestMapping("/viewRule")
	public ModelAndView viewRule(@RequestParam Integer ruleId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("rule", ruleDAO.findRuleByPrimaryKey(ruleId));
		mav.setViewName("rule/viewRule");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteRules")
	public ModelAndView confirmDeleteRules(
			@RequestParam(value = "deleteRulesIds", required = true) Set<Integer> deleteRulesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Rule> deleteRules = ruleDAO.findRulesByIds(deleteRulesIds);
		Set<Rule> referencedRules = ruleService.loadReferenceRules(deleteRules);
		boolean hasReferenced;
		if (referencedRules.size() == 0) {
			mav.addObject("deleteEntitys", deleteRules);
			hasReferenced = false;
		} else {
			deleteRules.removeAll(referencedRules);
			mav.addObject("referencedEntitys", referencedRules);
			mav.addObject("deleteEntitys", deleteRules);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Rule");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listRules");
		return mav;
	}

	@RequestMapping("/deleteRule")
	public String deleteRule(@RequestParam Integer ruleId) {
		Rule rule = ruleDAO.findRuleByPrimaryKey(ruleId);
		ruleService.deleteRule(rule);
		return "forward:/listRules";
	}

	@RequestMapping("/confirmDeleteRule")
	public ModelAndView confirmDeleteRule(@RequestParam Integer ruleId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("rule", ruleDAO.findRuleByPrimaryKey(ruleId));

		mav.setViewName("rule/deleteRule");

		return mav;
	}

	@RequestMapping("/deleteRules")
	public String deleteRules(@RequestParam(value = "deleteRulesId", required = true) Set<Integer> deleteRulesId) {
		ruleService.deleteRules(deleteRulesId);
		return "forward:/listRules";
	}

	@RequestMapping("/editRule")
	public ModelAndView editRule(@RequestParam Integer ruleId) {

		ModelAndView mav = new ModelAndView();

		Rule rule = ruleDAO.findRuleByPrimaryKey(ruleId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("rule", rule);

		// 把所有的引用rule对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(ruleService.loadRuleObjectReferencedSet(rule));

		// 将编辑页面rule添加到mav中
		mav.setViewName("rule/editRule");
		return mav;
	}

	@RequestMapping("/newRule")
	public ModelAndView newRule() {
		ModelAndView mav = new ModelAndView();
		Rule rule = new Rule();

		mav.addObject("rule", rule);

		// 把所有的引用rule对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(ruleService.loadRuleObjectReferencedSet(rule));

		mav.addObject("newFlag", true);
		mav.setViewName("rule/editRule");

		return mav;
	}

	@RequestMapping("/saveRule")
	public ModelAndView saveRule(@ModelAttribute Rule rule) {

		ModelAndView mav = new ModelAndView();

		Site siteTemp = rule.getSite();
		Site site = siteTemp == null ? null : CmiHibernateProxyUtil.deproxy(siteDAO.findSiteById(siteTemp.getId()));

		Rule existingRule = CmiHibernateProxyUtil
				.deproxy(ruleDAO.findRuleByPhysicalPrimaryKey(site.getId(), rule.getName()));

		rule.setSite(site);
		// 新建记录
		if (rule.getId() == null) {
			if (rule.equals(existingRule)) {

				mav.addAllObjects(ruleService.loadRuleClassReferencedSet(rule));
				// mav = editContinue(mav, rule);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("rule/editRule");
			} else {
				ruleService.saveRule(rule);
				mav.setViewName("forward:/listRules");
			}
		}
		// 编辑记录
		else {
			Rule originRule = CmiHibernateProxyUtil.deproxy(ruleDAO.findRuleByPrimaryKey(rule.getId()));
			if (!rule.equals(originRule) && rule.equals(existingRule)) {
				mav.addAllObjects(ruleService.loadRuleClassReferencedSet(rule));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("rule/editRule");
			} else {
				ruleService.saveRule(rule);
				mav.setViewName("forward:/listRules");
			}
		}
		return mav;
	}

	@RequestMapping("/ruleController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
